<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
        <!--

        function createXHR()
        {
            var request = false;
            try
            {
                request = new ActiveXObject('Msxml2.XMLHTTP');
            }
            catch (err2)
            {
                try
                {
                    request = new ActiveXObject('Microsoft.XMLHTTP');
                }
                catch (err3)
                {
                    try
                    {
                        request = new XMLHttpRequest();
                    }
                    catch (err1)
                    {
                        request = false;
                    }
                }
            }
            return request;
        }

        var topicSender;
        var nextMessage;
        var subscriptions;
        var count = 0;
        var reconnect = true;

        function initializeSenderAndTop(topic)
        {
            var xhr = createXHR();
            xhr.open("HEAD", "topics/" + topic, true);
            xhr.onreadystatechange = function()
            {
                if (xhr.readyState == 4)
                {
                    if (xhr.status == 200)
                    {
                       // getting the links from the rest resource
                       topicSender = xhr.getResponseHeader("msg-create");
                       subscriptions = xhr.getResponseHeader("msg-pull-subscriptions");

                       // just adding the report
                       document.getElementById("errors").innerHTML = "Subscriptions URL: " + subscriptions;
                    }
                }
            }
            
            // this will send the request from javascript
            xhr.send(null);
        }

        function postMessage(user, message)
        {
            var xhr = createXHR();
            xhr.open("POST", topicSender, false);
            xhr.setRequestHeader("Content-Type", "text/plain");
            xhr.send(user + ": " + message);
            if (xhr.status == 201)
            {
               topicSender = xhr.getResponseHeader("msg-create-next");
            }
            else
            {
               document.getElementById("errors").innerHTML = "Failed to send message: " + topicSender;
            }
        }

        function receiveMessage()
        {
            var xhr = createXHR();
            if (reconnect)
            {
               document.getElementById("connection").innerHTML = "Trying to reconnect: " + subscriptions + " retries: " + count++;
               xhr.open("POST", subscriptions, true);
               xhr.onreadystatechange = function()
               {
                  if (xhr.readyState == 4)
                  {
                    var status = xhr.status;
                    if (status == 201)
                    {
                       nextMessage = xhr.getResponseHeader("msg-consume-next");
                       document.getElementById("connection").innerHTML = "Connected to: " + nextMessage;
                       count = 1;
                       reconnect = false;
                    }
                    setTimeout("receiveMessage()", 800);
                  }
               }
               xhr.send(null);
            }
            else
            {
               xhr.open("POST", nextMessage, true);
               xhr.setRequestHeader("Accept-Wait", "10")
               xhr.onreadystatechange = function()
               {
                  if (xhr.readyState == 4)
                  {
                    var status = xhr.status;
                    if (status == 200)
                    {
                        document.getElementById("next").innerHTML = xhr.responseText + "\n" + document.getElementById("next").innerHTML;
                        nextMessage = xhr.getResponseHeader("msg-consume-next");
                    }
                    else
                    {
                       reconnect = true;
                    }
                    setTimeout("receiveMessage()", 800);
                  }
               }
               xhr.send(null);
            }
        }

        initializeSenderAndTop('chat');


        setTimeout("receiveMessage()", 800);


        // -->
    </script>
    <title>Ajax Chat Demo</title></head>


<body bgcolor="#FFFFFF">

<p><font size="+3">Ajax Chat Demo</font></p>
<hr>
<FORM name="ajax" method="POST" action="">

    <p>
        Username: <input type="text" name="user" value="Billy" size="10"><br/>
        <input type="text" name="message">
        <INPUT type="BUTTON" value=" Click to send message "
               ONCLICK="postMessage(this.form.user.value, this.form.message.value)">
    </p>

</FORM>
<div id="connection"></div>
<br>

<div id="errors"></div>

<p>

<h2>Messages:</h2>
<pre id="next">
</pre>

</body>
</html>
